package whu.iss.web.sys;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import whu.iss.pojo.UserCustom;
import whu.iss.services.impl.sys.LoginServices;

@WebServlet(value="/login")
public class LoginServlet extends HttpServlet
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String toPath="login.jsp";
		try
		{
			//1.获取页面数据
			String lname=request.getParameter("lname");
			String pwd=request.getParameter("pwd");
			
			//实例化Services
			LoginServices services=new LoginServices();
			//获取当前用户信息
			List<UserCustom> userInfo=services.checkUser(lname, pwd);
			if(userInfo!=null)
			{
				//将当前用户的信息存储到session中
				request.getSession().setAttribute("USERINFO", userInfo);
				request.getSession().setAttribute("CURRENTUSER",lname);
				
				toPath="main.jsp";
			}	

		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		request.getRequestDispatcher("/"+toPath).forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		this.doGet(request, response);
	}

}